﻿


var CalculatorTravelGuide = function (languageElement, genderElement, levelElement, travelGuideNameElement, travelGuideTypeElement, beginDateElement, endDateElement, totalDateElement, priceElement, priceTipElement, vatElement, totalPriceElement, beginDateTourElement, endDateTourElement, tourIdElement, quantityAdultsElement, quantityChildElement) {
    //ham load huoong dan vien
    var fncLoadTravelGuide = function(travelGuideCashId) {
        var language = $(languageElement).val();
        var gender = $(genderElement).val();
        var level = $(levelElement).val();
        if (language != -1 && gender != -1 && level != -1) {
            $.ajax({
                type: "POST",
                url: "/ajax.html",
                data: { "function": "TravelGuideListBy", "language": language, "gender": gender, "level": level },
                dataType: "json",
                success: function(data) {
                    $(travelGuideNameElement).html('<option value="-1">--- Chọn HDV ---</option>');
                    if (data && data.isError == false) {
                        $.each(data.array, function(index, item) {
                            $(travelGuideNameElement)
                                .append($("<option></option>")
                                    .attr("value", item.ID)
                                    .text(item.Name));
                        });
                        
                    }
                    $(travelGuideNameElement).val(travelGuideCashId);
                }
            });
        }
    };
    //ham tinh tien duong dan vien
    var fncTotalPriceTravelGuide = function () {
        var price = $(priceElement).val();
        var priceTip = $(priceTipElement).val();
        var quantityDate = $(totalDateElement).val();
        var vat = $(vatElement).val();
        var totalPriceVat = 0;
        if (price > 0 && priceTip > 0 && quantityDate > 0) {
            var totalprice = (parseInt(price) + parseInt(priceTip)) * quantityDate;
            totalPriceVat = totalprice;
            if (vat > 0 && vat <= 100) {
                totalPriceVat = totalprice + totalprice * (vat / 100);
            }
        }
        $(totalPriceElement).val(totalPriceVat);
    };
    
    $(languageElement).change(fncLoadTravelGuide);
    $(genderElement).change(fncLoadTravelGuide);
    $(levelElement).change(fncLoadTravelGuide);
    
    //ham tinh tong so ngay
    var fncQuantityDate = function() {
        if ($(beginDateElement).val() != "" && $(endDateElement).val() != "") {
            var dateBegin = $(beginDateElement).val().split("/");
            var dateEnd = $(endDateElement).val().split("/");
            var firstDate = new Date(dateBegin[2], dateBegin[1], dateBegin[0]);
            var secondDate = new Date(dateEnd[2], dateEnd[1], dateEnd[0]);
            var oneDay = 24 * 60 * 60 * 1000;
            var diffDays = Math.abs((firstDate.getTime() - secondDate.getTime()) / oneDay)+1;
            
            $(totalDateElement).val(diffDays);
            
        } else {
            $(totalDateElement).val('');
        }
        fncTotalPriceTravelGuide();
    };
    
    $(travelGuideTypeElement).change(function() {
        if ($(this).val() == 'Local') {
            $(beginDateElement).val('');
            $(endDateElement).val('');
        } else {
            $(beginDateElement).val(beginDateTourElement);
            $(endDateElement).val(endDateTourElement);
            fncQuantityDate();
        }
    });

    $(beginDateElement).change(fncQuantityDate);
    $(endDateElement).change(fncQuantityDate);
  
    
    $(travelGuideNameElement).change(function() {
        var travelGuideId = $(this).val();
        if (travelGuideId != -1) {
            $.ajax({
                type: "POST",
                url: "/ajax.html",
                data: { "function": "GetTravelGuide", "travelGuideId": travelGuideId },
                dataType: "json",
                success: function (data) {
                    $(priceElement).val(data.Price);
                    $(priceTipElement).val(data.PriceTip);
                    fncTotalPriceTravelGuide();
                }
            });
        }
    });
    $(vatElement).keyup(fncTotalPriceTravelGuide);
    
    $('#btnInsertTravelGuide').live("click", function () {
      
          var tourId = parseInt(tourIdElement);
        var ticketId = $(travelGuideNameElement).val();
        var typeId = 9;
        var beginDate = $(beginDateElement).val();
        var endDate = $(endDateElement).val();
        var priceAdult = $(priceElement).val();
        var priceChild = $(priceTipElement).val();
        var vat = $(vatElement).val();
        var travelGuideType = $(travelGuideTypeElement).val();
        var language = $(languageElement).val();
        var calculatorId = $(this).attr("calculatorid");
        var className = $(this).attr("class");
        $.ajax({
            type: "POST",
            url: "/ajax.html",
            data: { "function": "InsertTravelGuide", "typeId": typeId, "tourId": tourId, "ticketId": ticketId, "priceAdult": priceAdult, "priceChild": priceChild, "beginDate": beginDate, "endDate": endDate, "calculatorId": calculatorId, "className": className, 'vat': vat, "travelGuideType": travelGuideType, "language": language, 'QuantityAdults': quantityAdultsElement, 'QuantityChild': quantityChildElement },
            dataType: "json",
            success: function (data) {

                $(".ListTravelGuide").remove();
                $(travelGuideTypeElement).val('-1');
                $(beginDateElement).val('');
                $(endDateElement).val('');
                $(totalDateElement).val('');
                $(languageElement).val('');
                $(genderElement).val('');
                $(levelElement).val('');
                $(travelGuideNameElement + ' > option').remove();
                $(priceElement).val('');
                $(priceTipElement).val('');
                $(totalPriceElement).val('');
                $(vatElement).val('');
                if (data && data.isError == false) {
                    $.each(data.array, function (index, item) {
                        $('#new-travelGuide')
                            .before($("<tr class='ListTravelGuide'></tr>").attr("id", 'TravelGuide-' + item.CalculatorId).html('<td></td>' +
                                '<td >' + item.TravelGuideType + '</td>' +
                                '<td >' + item.BeginDate + '</td>' +
                                '<td >' + item.BeginEnd + '</td>' +
                                '<td >' + item.QuantityDate + '</td>' +
                                '<td >' + item.LanguageName + '</td>' +
                                '<td >' + item.Gender + '</td>' +
                                '<td >' + item.Level + '</td>' +
                                '<td>' + item.TravelGuideName + '</td>' +
                                '<td >' + item.Price + '</td>' +
                                '<td >' + item.PriceTip + '</td>' +
                                '<td >' + item.Vat + '</td>' +
                                '<td >' + item.TotalPrice + '</td>' +
                                '<td></td>' +
                                '<td>' +
                                '<input class="btnUpdateTravelGuide"  type="button" value="Cập nhật" calculatorid="' + item.CalculatorId + '"   />' +
                                '<input class="btnDeleteTravelGuide"  type="button" value="Xóa" calculatorid="' + item.CalculatorId + '"   />' +
                                '</td>'));

                    });
           
                    $('#TotalTravelGuidePrices').html(data.TotalTravelGuidePrices);
                    $('#TotalTravelGuidePricesAdults').html(data.TotalTravelGuidePricesAdults);
                    $('#TotalTravelGuidePricesAdults').html(data.TotalTravelGuidePricesAdults);
                    $('#btnInsertTravelGuide').attr("calculatorid", "-1").val("Thêm");
                    $('.btnUpdateTravelGuide').show();
                    $('#btnDeleteTravelGuide').hide();
                }

            }
        });
    });
    $('.btnDeleteTravelGuide').live("click", function () {
        var tourId = parseInt(tourIdElement);
        var ticketId = $(travelGuideNameElement).val();
        var typeId = 9;
        var beginDate = $(beginDateElement).val();
        var endDate = $(endDateElement).val();
        var priceAdult = $(priceElement).val();
        var priceChild = $(priceTipElement).val();
        var vat = $(vatElement).val();
        var travelGuideType = $(travelGuideTypeElement).val();
        var language = $(languageElement).val();
        var calculatorId = $(this).attr("calculatorid");
        var className = $(this).attr("class");
        $.ajax({
            type: "POST",
            url: "/ajax.html",
            data: { "function": "InsertTravelGuide", "typeId": typeId, "tourId": tourId, "ticketId": ticketId, "priceAdult": priceAdult, "priceChild": priceChild, "beginDate": beginDate, "endDate": endDate, "calculatorId": calculatorId, "className": className, 'vat': vat, "travelGuideType": travelGuideType, "language": language, 'QuantityAdults': quantityAdultsElement, 'QuantityChild': quantityChildElement },
            dataType: "json",
            success: function (data) {

                $(".ListTravelGuide").remove();
                $(travelGuideTypeElement).val('-1');
                $(beginDateElement).val('');
                $(endDateElement).val('');
                $(totalDateElement).val('');
                $(languageElement).val('');
                $(genderElement).val('');
                $(levelElement).val('');
                $(travelGuideNameElement + ' > option').remove();
                $(priceElement).val('');
                $(priceTipElement).val('');
                $(totalPriceElement).val('');
                $(vatElement).val('');
                if (data && data.isError == false) {
                    $.each(data.array, function (index, item) {
                        $('#new-travelGuide')
                            .before($("<tr class='ListTravelGuide'></tr>").attr("id", 'TravelGuide-' + item.CalculatorId).html('<td></td>' +
                                '<td >' + item.TravelGuideType + '</td>' +
                                '<td >' + item.BeginDate + '</td>' +
                                '<td >' + item.BeginEnd + '</td>' +
                                '<td >' + item.QuantityDate + '</td>' +
                                '<td >' + item.LanguageName + '</td>' +
                                '<td >' + item.Gender + '</td>' +
                                '<td >' + item.Level + '</td>' +
                                '<td>' + item.TravelGuideName + '</td>' +
                                '<td >' + item.Price + '</td>' +
                                '<td >' + item.PriceTip + '</td>' +
                                '<td >' + item.Vat + '</td>' +
                                '<td >' + item.TotalPrice + '</td>' +
                                '<td></td>' +
                                '<td>' +
                                '<input class="btnUpdateTravelGuide"  type="button" value="Cập nhật" calculatorid="' + item.CalculatorId + '"   />' +
                                '<input class="btnDeleteTravelGuide"  type="button" value="Xóa" calculatorid="' + item.CalculatorId + '"   />' +
                                '</td>'));
                    });
                    $('#TotalTravelGuidePrices').html(data.TotalTravelGuidePrices);
                    $('#TotalTravelGuidePricesAdults').html(data.TotalTravelGuidePricesAdults);
                    $('#TotalTravelGuidePricesChild').html(data.TotalTravelGuidePricesChild);
                    
                    $('#btnInsertTravelGuide').attr("calculatorid", "-1").val("Thêm");
                    $('.btnUpdateTravelGuide').show();
                    $('#btnDeleteTravelGuide').hide();
                }

            }
        });
    });
    $('.btnUpdateTravelGuide').live("click", function () {
        var calculatorId = $(this).attr("calculatorid");

        $.ajax({
            type: "POST",
            url: "/ajax.html",
            data: { "function": "UpdateTravelGuide", "CalculatorId": calculatorId },
            dataType: "json",
            success: function (data) {
                $('#TravelGuide-' + calculatorId).remove();
                $(travelGuideTypeElement).val(data.TravelGuideType);
                $(beginDateElement).val(data.BeginDate);
                $(endDateElement).val(data.BeginEnd);
                $(totalDateElement).val(data.QuantityDate);
                $(languageElement).val(data.LanguageId);
                $(genderElement).val(data.Gender);
                $(levelElement).val(data.Level);
                fncLoadTravelGuide(data.TravelGuideCashId);
                $(priceElement).val(data.Price);
                $(priceTipElement).val(data.PriceTip);
                $(vatElement).val(data.Vat);
                $(totalPriceElement).val(data.TotalPrice);
                $('#btnInsertTravelGuide').attr("calculatorid", calculatorId).val("Lưu");
                $('.btnUpdateTravelGuide').hide();
                $('#btnDeleteTravelGuide').show();
            }
        });


    });
};